using System;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.VideoSurveillance;
using NIPO.Data;
using NIPO.Plugins;
using NIPO.Util;

namespace NIPOCvPlugins.BGSubtractPlugin
{
    public class BGSubtactionProcessingPlugin:ProcessingPlugin
    {
        private static IBGFGDetector<Bgr> _detector;


        #region Overrides of Plugin

        protected override void Initialize()
        {
            _detector = new FGDetector<Bgr>(FORGROUND_DETECTOR_TYPE.FGD);     
        }

        public override void Dispose()
        {
            _detector = null;
        }

        public override bool IsConfigured
        {
            get { return true; }
        }

        public override void Reset()
        {
            _detector = null;
        }

        #endregion

        #region Overrides of ProcessingPlugin

        public override void ProcessData(object sender, NIPOEventArgs e)
        {
            Image<Bgr, byte> image = (e.Data as GenericDTO<Image<Bgr, byte>>).Value;
            lock (this)
            {
                _detector.Update(image);
                RaiseNewData(new GenericDTO<Image<Gray, byte>>(_detector.ForgroundMask, "EMGU/Image<Gray, byte>",
                                                               _detector.ForgroundMask.Bytes.Length, DateTime.Now)); 
            }
        }

        #endregion
    }
}